package com.ia.aistream.service.dxfcomponent.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ia.aistream.common.utils.DateUtils;
import com.ia.aistream.manager.common.ICommonFileManager;
import com.ia.aistream.manager.dxfcomponent.conf.DrawMapConf;
import com.ia.aistream.manager.dxfcomponent.util.FileUtilTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ia.aistream.mapper.dxfcomponent.DxfDrawMapper;
import com.ia.aistream.model.dxfcomponent.entity.DxfDraw;
import com.ia.aistream.service.dxfcomponent.IDxfDrawService;
import com.ia.aistream.common.core.text.Convert;

/**
 * dxf文件索引信息Service业务层处理
 * 
 * @author ymj
 * @date 2020-12-04
 */
@Service
public class DxfDrawServiceImpl implements IDxfDrawService
{
    @Autowired
    private DxfDrawMapper dxfDrawMapper;


    /**
     * 查询dxf文件索引信息
     * 
     * @param drawId dxf文件索引信息ID
     * @return dxf文件索引信息
     */
    @Override
    public DxfDraw selectDxfDrawById(Long drawId)
    {
        return dxfDrawMapper.selectDxfDrawById(drawId);
    }

    /**
     * 查询dxf文件索引信息列表
     * 
     * @param dxfDraw dxf文件索引信息
     * @return dxf文件索引信息
     */
    @Override
    public List<DxfDraw> selectDxfDrawList(DxfDraw dxfDraw)
    {
        return dxfDrawMapper.selectDxfDrawList(dxfDraw);
    }

    @Override
    public List<DxfDraw> checkDxfDrawList(DxfDraw dxfDraw) {
        return dxfDrawMapper.checkDxfDrawList(dxfDraw);
    }

    /***
     * 检查文件是否存在
     */

    /**
     * 新增dxf文件索引信息
     * 
     * @param dxfDraw dxf文件索引信息
     * @return 结果
     */
    @Override
    public int insertDxfDraw(DxfDraw dxfDraw)
    {

        /***
         * 进行唯一性验证
         */
        DxfDraw dxTest = new DxfDraw();
        dxTest.setFileCode(dxfDraw.getFileCode());
        dxTest.setVersion(dxfDraw.getVersion());
        dxTest.setEdition(dxfDraw.getEdition());
        // 查询数据库中，是否已经存在该索引对象
        List<DxfDraw> dxfDraws = dxfDrawMapper.selectDxfDrawList(dxTest);

        // 判断dxfDraws的大小，如果集合大小为0，则正常插入
        if(dxfDraws!=null && dxfDraws.size()==0){
            dxfDraw.setCreateTime(DateUtils.getNowDate());
            return dxfDrawMapper.insertDxfDraw(dxfDraw);
        }

        return 0;
    }

    /***
     * 1、将dwg文件转为JPG文件，注意文件的大小
     * 2、将dwg文件转化为dxf文件
     * 3、
     * @param fileName
     * @return
     */
    @Override
    public Map<String, Object> fileAnalysis(String fileName) {
        //将dwg文件转化为JPG文件
//        String imageFile = CadFileUtil.cadFileToImage(fileName, 0.0f, 0.0f);
        //将dwg文件转化为dxf文件

//        String dxfFile = "D:/project/CAD/1-25/FQ1-APG-LC-002_蒸汽发生器排污系统流程图（二）001_06.dxf";
//        //解析DXF文件
//        Map<String, Object> map = FileUtilTest.getDxfResultEquipment(dxfFile);
//        map.put("images",imageFile);
        return null;
    }

    /**
     * 修改dxf文件索引信息
     * 
     * @param dxfDraw dxf文件索引信息
     * @return 结果
     */
    @Override
    public int updateDxfDraw(DxfDraw dxfDraw)
    {
        dxfDraw.setUpdateTime(DateUtils.getNowDate());
        return dxfDrawMapper.updateDxfDraw(dxfDraw);
    }

    /**
     * 删除dxf文件索引信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDxfDrawByIds(String ids)
    {
        return dxfDrawMapper.deleteDxfDrawByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除dxf文件索引信息信息
     * 
     * @param drawId dxf文件索引信息ID
     * @return 结果
     */
    @Override
    public int deleteDxfDrawById(Long drawId)
    {
        return dxfDrawMapper.deleteDxfDrawById(drawId);
    }

    @Override
    public Map<String, Object> getDxfDrawService(Long drawId) {

        Map<String, Object> map = new HashMap<String, Object>();
        DxfDraw dxfDraw = dxfDrawMapper.selectDxfDrawById(drawId);

        map.put(DrawMapConf.DXF_IMAGES.value, dxfDraw.getDxfImg());
        map.put(DrawMapConf.DWG_DXF_INDEX.value, dxfDraw);
        return map;
    }
}
